<?php
namespace App\Query;

use App\Model\Order;
use App\Query\Traits\UserTrait;

class OrderQuery extends \HyperfStore\Query
{
    use UserTrait;
    
    // 构建模型
    protected static function build()
    {
        return Order::latest();
    }
    
    // 渴望加载
    protected static function with()
    {
        return ['user', 'shop', 'daili'];
    }
    
    // 时间区间
    protected static function queryTime($model, $where)
    {
        return $model->whereBetween('created_at', $where);
    }
    
    // 商品名称订单搜索
    protected static function queryProductName($model, $where)
    {
        return $model->where('product_name', 'like', '%' . $where . '%');
    }
    
    // 所属代理
    public static function daili($daili_id)
    {
        $model = self::model();
        
        if($daili_id)
        {
            $model->where('daili_id', $daili_id);
        }
        
        return $model;
    }
    
    // 查询时间段购买的数量
    public static function dayCount($model, $time)
    {
        return static::queryTime($model, [$time, time()])->sum('count');
    }
}